package cn.zurish.cloud.work.controller;

import cn.zurish.cloud.work.dao.entity.Temperature;
import cn.zurish.cloud.work.dao.entity.WorkTimes;
import cn.zurish.cloud.work.dao.mapper.TemperatureMapper;
import cn.zurish.cloud.work.dao.mapper.WorkTimesMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    private TemperatureMapper temperatureMapper;

    @Autowired
    private WorkTimesMapper workTimesMapper;

    /**
     * 获取温度数据列表（分页）
     *
     * @param page 页码
     * @param size 每页大小
     * @return 温度数据列表
     */
    @GetMapping("/temperatures")
    public Map<String, Object> getTemperatureList(@RequestParam(defaultValue = "1") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer size) {
        Page<Temperature> pageData = new Page<>(page, size);
        Page<Temperature> result = temperatureMapper.selectPage(pageData, null);

        Map<String, Object> response = new HashMap<>();
        response.put("data", result.getRecords());
        response.put("total", result.getTotal());
        response.put("current", result.getCurrent());
        response.put("pages", result.getPages());

        return response;
    }

    /**
     * 获取最新的一条温度数据
     *
     * @return 最新的温度数据
     */
    @GetMapping("/temperature/latest")
    public Temperature getLatestTemperature() {
        QueryWrapper<Temperature> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return temperatureMapper.selectOne(queryWrapper);
    }

    /**
     * 获取工位运行时间数据列表（分页）
     *
     * @param page 页码
     * @param size 每页大小
     * @return 工位运行时间数据列表
     */
    @GetMapping("/work-times")
    public Map<String, Object> getWorkTimesList(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size) {
        Page<WorkTimes> pageData = new Page<>(page, size);
        Page<WorkTimes> result = workTimesMapper.selectPage(pageData, null);

        Map<String, Object> response = new HashMap<>();
        response.put("data", result.getRecords());
        response.put("total", result.getTotal());
        response.put("current", result.getCurrent());
        response.put("pages", result.getPages());

        return response;
    }

    /**
     * 获取最新的一条工位运行时间数据
     *
     * @return 最新的工位运行时间数据
     */
    @GetMapping("/work-time/latest")
    public WorkTimes getLatestWorkTime() {
        QueryWrapper<WorkTimes> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return workTimesMapper.selectOne(queryWrapper);
    }
}